﻿using System;
using System.Diagnostics;
using System.Linq;
using FellowshipOne.API.Events.Model;
using FellowshipOne.API.Events.QueryObject;
using FellowshipOne.API.Groups.Model;
using FellowshipOne.API.Groups.QueryObject;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;


namespace FellowshipOne.API.Tests
{
    [TestClass]
    public class AttendanceTests : Base
    {
        #region Pruebas
        [TestMethod]
        public void GetAttendanceSummary()
        {
            var attendanceSummary = Client.EventsRealm.AttendanceSummary.Get("2415422");

            attendanceSummary.ShouldNotBe(null);
        }

        [TestMethod]
        public void CreateAttendaceSummary()
        {
            AttendanceSummary asummary = new AttendanceSummary();
            asummary.CreatedDate = DateTime.Now.AddDays(1);
            asummary.StartDateTime = DateTime.Now.AddDays(1);
            asummary.EndDateTime = DateTime.Now.AddDays(1);
            asummary.AttendanceContextType = new AttendanceContextType() { ID = 1 };
            asummary.AttendanceContextValueID = "1212115";

            Client.EventsRealm.AttendanceSummary.Create(asummary);
        }


        [TestMethod]
        public void UpdateAttendanceSummary()
        {
            var attendanceSummaryId = "2415943";
            AttendanceSummary attendanceSummary = Client.EventsRealm.AttendanceSummary.Get(attendanceSummaryId);
            attendanceSummary.PresentCount = 4;
            attendanceSummary.Met = false;
            attendanceSummary.IsPosted = true;
            attendanceSummary.Comments = "No quizimos y que!!!!";

            string requestXml = string.Empty;
            Client.EventsRealm.AttendanceSummary.Update(attendanceSummary, attendanceSummary.ID.ToString(), out requestXml);

        }

        [TestMethod]
        public void GetAttendance()
        {
            var attendance = Client.EventsRealm.Attendance.Get("2415880", "36951967");
            attendance.ShouldNotBe(null);
        }

        [TestMethod]
        public void UpdateAttendance()
        {
            var parentId = "2415422";
            var attendance = Client.EventsRealm.Attendance.Get(parentId, "36946882");
            attendance.IsPresent = true;
            attendance.UpdatedDateString = string.Empty;

            string requestXml = string.Empty;
            Client.EventsRealm.Attendance.Update(attendance, attendance.ID.ToString(), attendance.AttendanceSummary.ID.ToString(), out requestXml);

        }

        #endregion

        #region Primary workflow

        [TestMethod]
        public void GetGroupIdForLeader()
        {
            var person = Client.PeopleRealm.People.GetByUrl(Ticket.PersonURL);

            var query = new MemberQO();
            if (person.ID != null) query.PersonId = person.ID.Value;

            var members = Client.GroupsRealm.Member.Search<MemberCollection>(query);

            members.Count.ShouldBeGreaterThan(0);

            Debug.WriteLine(members[0].Group.ID);
        }

        [TestMethod]
        public void GetAllAttendancesForGroup()
        {
            // Group 1212115
            var query = new AttendanceSummaryQO();
            query.AttendanceContextTypeId = 1;
            query.AttendanceContextValueId = 1212115;

            var attendanceSummaries = Client.EventsRealm.AttendanceSummary.Search<AttendanceSummaryCollection>(query);
            attendanceSummaries.Count.ShouldBeGreaterThan(0);

            var mostRecentAttendanceSummary = attendanceSummaries.OrderByDescending(a => a.CreatedDate).First();

            if (mostRecentAttendanceSummary != null)
            {
                Debug.WriteLine(mostRecentAttendanceSummary.ID);
            }
        }

        [TestMethod]
        public void UpdateAttendances()
        {
            // Attendance Summary 2416269

            // Update AttendanceSummary
            var attendanceSummaryId = "2416269";
            var attendanceSummary = Client.EventsRealm.AttendanceSummary.Get(attendanceSummaryId);
            attendanceSummary.PresentCount = 4;
            attendanceSummary.Met = true;
            attendanceSummary.IsPosted = true;

            Client.EventsRealm.AttendanceSummary.Update(attendanceSummary, attendanceSummary.ID.ToString());

            // Update attendances
            var attendances = Client.EventsRealm.AttendanceCollection.Get(attendanceSummaryId);
            foreach (var attendance in attendances)
            {
                attendance.IsPresent = true;
                attendance.UpdatedDateString = string.Empty;

                Client.EventsRealm.Attendance.Update(attendance, attendance.ID.ToString(), attendanceSummaryId);
            }
        }


        #endregion
    }
}
